package deal2;

import com.opencsv.CSVReader;
import com.opencsv.CSVWriter;
import com.opencsv.exceptions.CsvException;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class MergeCSVFiles {

    public static void merge(String inputFile1,String inputFile2,String outputFile) throws CsvException {
        mergeCSVFiles(inputFile1, inputFile2, outputFile);
    }

    private static void mergeCSVFiles(String inputFile1, String inputFile2, String outputFile) throws CsvException {
        try (CSVReader reader1 = new CSVReader(new FileReader(inputFile1));
             CSVReader reader2 = new CSVReader(new FileReader(inputFile2));
             CSVWriter writer = new CSVWriter(new FileWriter(outputFile))) {

            // 读取第一个文件的内容
            List<String[]> lines1 = reader1.readAll();

            // 写入第一个文件的内容到输出文件
            writer.writeAll(lines1);

            // 读取第二个文件的内容，跳过第一行（假设第一行是标题行）
            List<String[]> lines2 = reader2.readAll();
            lines2.remove(0);

            // 写入第二个文件的内容到输出文件
            writer.writeAll(lines2);

            System.out.println("CSV文件已成功合并。");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
